﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.DAL;
using MINERP.MODEL.Statistics;
using MINERP.BLL.Statistics;
using MINERP.COM;
using MINERP.ComControls;

namespace MINERP.Statistics
{
    public partial class FrmFinance:Form
    {
        FinanceBLL _bll = new FinanceBLL();
        private ShopEntity _shopEntity = null;
        private static FrmFinance instance = null;
        int rowhandle = 0;
        private FrmFinance()
        {
            InitializeComponent();
        }

        public static FrmFinance GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmFinance();
                }
                return instance;
            }
        
        }

        private void FrmFinance_Load(object sender, EventArgs e)
        {
            try
            {

                BlanceOprationRecord.CreateLogFile();
                LinkDataGrid();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void FrmFinance_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void gridViewShop_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _shopEntity = (ShopEntity)this.gridViewShop.GetRow(e.FocusedRowHandle);
        }

        private void gridViewShop_DoubleClick(object sender, EventArgs e)
        {
            //FrmPay.shopid = _shopEntity.ShopId;
            //FrmPay frmPay = FrmPay.GetInstance;
            //frmPay.WindowState = FormWindowState.Maximized;
            //frmPay.ShowDialog();
        }

        private void tsbtnshopBlance_Click(object sender, EventArgs e)
        {
            FrmShopBlance.frmFinance = instance;
            _shopEntity = (ShopEntity)this.gridViewShop.GetFocusedRow();
            FrmShopBlance._shopEntity = _shopEntity;
            FrmShopBlance frmShopBlance = new FrmShopBlance();
            frmShopBlance.ShowDialog();
        }

        private void tsbtnchongzhi_Click(object sender, EventArgs e)
        {
            FrmPay.frmFinance = instance;
            _shopEntity = (ShopEntity)this.gridViewShop.GetFocusedRow();
            FrmPay.shopid = _shopEntity.ShopId;
            FrmPay frmPay = FrmPay.GetInstance;
            frmPay.ShowDialog();
        }

        private void tsbtnBill_Click(object sender, EventArgs e)
        {

        }

        public void LinkDataGrid()
        {
            gridControlShop.DataSource = _bll.GetShopData();
            gridViewShop.FocusedRowHandle = rowhandle;
            gridControlShop.RefreshDataSource();
        }

        private void gridViewShop_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string savePath = folderBrowserDialog1.SelectedPath + "\\商户列表.xls";
                this.Cursor = Cursors.WaitCursor;
                gridControlShop.MainView.ExportToXls(savePath);
                this.Cursor = Cursors.Default;
                MessageBox.Show("已成功导出到:" + savePath, "成功导出", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMnemoniccode.Text.Trim()))
            {
                gridControlShop.DataSource = _bll.GetShopDataByMnemoniccode(txtMnemoniccode.Text);
                gridControlShop.RefreshDataSource();
            }
            else
            {
                txtMnemoniccode.SelectAll();
                MessageBox.Show("请输入助记码","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void gridViewShop_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            rowhandle = e.RowHandle;
        }

        private void tsbtnRefreshOrderBalance_Click(object sender, EventArgs e)
        {
            tsbtnRefreshOrderBalance.Enabled = false;
            Cursor = Cursors.WaitCursor;
            try
            {
                List<ShopEntity> shopList = (List<ShopEntity>)gridControlShop.DataSource;
                foreach (var item in shopList)
                {
                    try
                    {
                         _bll.GetUnCompleteProductBillByShop(item.ShopId);

                    }
                    catch (Exception ex)
                    {
                        if(ex is NullReferenceException)
                        {
                            //有产品未设置价格
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                }
                LinkDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            finally
            {
                tsbtnRefreshOrderBalance.Enabled = true;
                Cursor = Cursors.Default;
            }
        }
    }
}
